<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: ZJZ
 * @Date: 2021-07-26 14:02:31
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-06-01 15:34:30
-->
<template>
  <div class="app-container">
    <el-drawer
      title="详细信息"
      :visible.sync="dialog.clueDrawer"
      :direction="direction"
      :with-header="false"
      size="60%"
      :modal="false"
      @close="drawerClose"
    >
      <div class="head">
        <div class="header">
          <div class="img">
            <img src="@/assets/images/icon-batch.png" alt="" />
            <div class="information">
              <span>线索名称</span>
              <h4>{{ drawerData.name }}</h4>
            </div>
          </div>
          <div class="btn">
            <el-button
              type="primary"
              @click="edit"
              v-hasPermi="['serviceProvide:clue:update']"
            >
              编辑
            </el-button>
            <el-button
              plain
              @click="changeCharge"
              v-hasPermi="['serviceProvide:clue:changeDirector']"
            >
              更换负责人
            </el-button>
            <el-dropdown style="margin-left: 10px">
              <el-button class="el-icon-more"></el-button>
              <el-dropdown-menu slot="dropdown">
                <!-- <el-dropdown-item> 导入 </el-dropdown-item> -->
                <el-dropdown-item @click.native="selectImport">
                  导出
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="footer">
          <div class="information">
            <span>线索来源</span>
            <h4>{{ drawerData.sourceName }}</h4>
          </div>
          <div class="information">
            <span>手机号码</span>
            <h4>{{ drawerData.phone }}</h4>
          </div>
          <div class="information">
            <span>负责人</span>
            <h4>{{ drawerData.directorName }}</h4>
          </div>
        </div>
      </div>
      <div class="strip"></div>
      <div class="specific">
        <el-tabs
          v-model="activeName"
          type="border-card"
          v-if="dialog.clueDrawer"
        >
          <el-tab-pane label="活动" name="first" style="height: 100vh">
            <activity
              class="activ"
              :drawerData="drawerData"
              :furtherData="furtherData"
              :dialog="dialog"
              :getfurther="getfurther"
              :activityForm="activityForm"
              :getEnclosure="getEnclosure"
            ></activity>
          </el-tab-pane>
          <el-tab-pane label="详细资料" name="second">
            <div class="basics">基础信息</div>
            <div class="basics-information">
              <div class="subclass">
                <span>线索名称：{{ drawerData.name }}</span>
              </div>
              <div class="subclass">
                <span>手机号码：{{ drawerData.phone }}</span>
              </div>
              <div class="subclass">
                <span>客户行业：{{ drawerData.industryName }}</span>
              </div>
              <div class="subclass">
                <span>线索来源：{{ drawerData.sourceName }}</span>
              </div>
              <div class="subclass">
                <span>客户级别：{{ drawerData.levelName }}</span>
              </div>
              <div class="subclass">
                <span>地址：{{ drawerData.address }}</span>
              </div>
              <div class="subclass">
                <span>备注：{{ drawerData.remark }} </span>
              </div>
            </div>
            <div class="system">系统信息</div>
            <div class="basics-information">
              <div class="subclass">
                <span>创建人：{{ drawerData.createBy }}</span>
              </div>
              <div class="subclass">
                <span>负责人：{{ drawerData.directorName }}</span>
              </div>
              <div class="subclass">
                <span>创建时间：{{ drawerData.createTime }}</span>
              </div>
              <div class="subclass">
                <span>更新时间：{{ drawerData.updateTime }}</span>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane label="附件" name="third">
            <el-upload
              class="upload-demo"
              ref="uploadFile"
              action="#"
              accept=".xlsx,.xls,.doc,.docx,.pdf"
              :file-list="fileList"
              :on-change="getFile1"
              :auto-upload="false"
              :limit="1"
              :on-remove="handleRemove1"
              style="margin-top: 10px"
            >
              <el-button
                class="taskBtn"
                v-hasPermi="['serviceProvide:clue:importEnclosure']"
              >
                上传附件
              </el-button>
            </el-upload>
            <el-table :data="enclosureData">
              <template slot="empty" class="emptyBg">
                <img src="@/assets/images/empty.jpg" alt="" />
              </template>
              <el-table-column label="附件名称" prop="fileName">
              </el-table-column>
              <el-table-column label="附件大小" prop="size"> </el-table-column>
              <el-table-column label="上传人" prop="createBy">
              </el-table-column>
              <el-table-column label="上传时间" prop="createTime">
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <span
                    class="cz"
                    @click="preview(scope.row)"
                    v-hasPermi="['serviceProvide:clue:preview']"
                    >预览</span
                  >
                  <a :href="scope.row.fileUrl" class="cz" v-hasPermi="['']"
                    >下载</a
                  >
                  <span
                    class="cz"
                    @click="logShow(scope.row)"
                    v-hasPermi="['serviceProvide:clue:rename']"
                    >重命名</span
                  >
                  <span
                    class="del"
                    @click="del(scope.row)"
                    v-hasPermi="['serviceProvide:clue:delEnclosure']"
                    >删除</span
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="团队人员" name="seventh">
            <div class="team">
              <div class="team-right">
                <el-button type="primary" size="default" plain @click="addTeam">
                  添加团队成员
                </el-button>
                <el-button type="primary" size="default" plain @click="outTeam">
                  退出团队
                </el-button>
              </div>
              <div class="team-left" v-if="selShow">
                <el-button
                  type="primary"
                  size="default"
                  plain
                  :disabled="tDisabled"
                  @click="editTeam"
                >
                  编辑
                </el-button>
                <el-button
                  type="primary"
                  size="default"
                  plain
                  @click="removeTeam"
                >
                  移除
                </el-button>
              </div>
            </div>
            <el-table
              :data="teamData"
              height="3rem"
              @selection-change="teamChange"
            >
              <template slot="empty" class="emptyBg">
                <img src="@/assets/images/empty.jpg" alt="" />
              </template>
              <el-table-column type="selection" width="55"> </el-table-column>
              <el-table-column label="姓名" prop="nickName"> </el-table-column>
              <el-table-column label="部门" prop="deptName"> </el-table-column>
              <el-table-column label="团队角色">
                <template slot-scope="scope">
                  {{ scope.row.isAdmin == true ? "负责人" : "成员" }}
                </template>
              </el-table-column>
              <el-table-column label="有效时间" prop="expire">
                <template slot-scope="scope">
                  {{ scope.row.expire == null ? "不限" : scope.row.expire }}
                </template>
              </el-table-column>
              <el-table-column label="权限">
                <template slot-scope="scope">
                  {{
                    scope.row.isAdmin == true
                      ? "负责人"
                       : scope.row.readonly == 0
                      ? "读写"
                      : "只读"
                  }}
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="操作记录" name="fourth">
            <div
              class="operation"
              v-for="(item, index) in Operation"
              :key="index"
            >
              <div class="user">
                <div class="portrait">
                  <img :src="item.avatar" alt="" />
                </div>
                <el-tooltip
                  class="item"
                  effect="dark"
                  :content="item.createBy"
                  placement="top"
                >
                  <div class="name">{{ item.createBy }}</div>
                </el-tooltip>
              </div>
              <div class="record">
                <div class="time">{{ item.createTime }}</div>
                <div class="content">
                  <div>{{ item.content }}</div>
                </div>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-drawer>
    <el-dialog
      title="重命名"
      :visible.sync="renameShow"
      width="30%"
      style="margin-top: 200px; margin-left: 600px"
      custom-class="dialogClass"
      :close-on-click-modal="false"
    >
      <el-input
        v-model="input"
        placeholder="请输入文件名"
        size="normal"
      ></el-input>
      <span slot="footer">
        <el-button @click="renameShow = false">取消</el-button>
        <el-button type="primary" @click="rename">保存</el-button>
      </span>
    </el-dialog>
    <el-dialog title="预览" :visible.sync="show" :fullscreen="true">
      <div v-html="fileHtml" style="text-align: center"></div>
    </el-dialog>
    <edit-user
      :dialog="dialog"
      :teamForm="teamForm"
      v-if="dialog.editTeamShowEject"
    ></edit-user>
  </div>
</template>

<script>
import Activity from "./activity.vue";
import {
  getActivity,
  getEnclosure,
  delEnclosure,
  rename,
  preview,
  OperationRecord,
  importEnclosureClue,
  exportSelect,
} from "@/api/CRM";
import { teamList, deleteTeam, withdrawTeam } from "@/api/team";
import EditUser from "@/components/joinTeam/editUser.vue";
export default {
  components: { Activity, EditUser },
  props: {
    dialog: {
      type: Object,
      required: true,
    },
    drawerData: {
      type: Object,
      required: true,
    },
  },

  data() {
    return {
      teamIds: [],
      teamForm: {},
      selShow: false,
      tDisabled: true,
      teamMul: [],
      teamData: [],
      fileList: [],
      show: false,
      renameShow: false,
      fileHtml: "",
      input: "",
      id: "",
      Operation: [],
      activityForm: {
        type: "",
        contactTime: "",
        content: "",
        crmId: this.drawerData.id,
        crmType: this.drawerData.crmType,
      },
      furtherData: [],
      enclosureData: [],
      activeName: "first",
      direction: "rtl",
    };
  },
  watch: {
    drawerData: {
      deep: true,
      immediate: true,
      handler() {
        if (this.drawerData !== {}) {
          this.getfurther();
          this.getOperationList();
          this.getEnclosure();
          this.getTeamList();
        }
      },
    },
  },
  created() {},
  methods: {
    // 退出团队
    outTeam() {
      withdrawTeam({
        module: 8,
        entity: this.drawerData.id,
        userId: this.drawerData.userId,
      }).then((res) => {
        this.$message.success(res.msg);
        this.getTeamList();
      });
    },
    // 移除团队成员
    removeTeam() {
      deleteTeam({
        userId: this.drawerData.userId,
        ids: this.teamIds,
      }).then((res) => {
        this.$message.success(res.msg);
        this.getTeamList();
      });
    },
    // 编辑团队列表
    editTeam() {
      this.dialog.editTeamShowEject = true;
      this.$nextTick(function () {
        this.dialog.editTeamShow = true;
      });
    },
    // 团队列表选中
    teamChange(val) {
      this.teamMul = val;
      this.teamIds = [];
      if (val.length !== 0) {
        this.selShow = true;
        this.teamForm = val[0];
        val.forEach((item) => {
          this.teamIds.push(item.id);
        });
      } else {
        this.selShow = false;
      }
      if (val.length > 1) {
        this.tDisabled = true;
      } else {
        this.tDisabled = false;
      }
    },
    // 添加团队成员
    addTeam() {
      this.$parent.entities.push(this.drawerData.id)
      this.$parent.addTeam();
    },
    // 获取团队成员
    getTeamList() {
      teamList({
        module: 8,
        entity: this.drawerData.id,
        userId: this.drawerData.userId,
      }).then((res) => {
        this.teamData = res.data;
      });
    },
    selectImport() {
      let ids = [];
      this.ids.push(this.drawerData.id);
      exportSelect(ids).then((res) => {
        this.download(res.msg);
      });
    },
    quitClose() {
      this.dialog.clueDrawer = false;
    },
    handleRemove1(file, fileList) {
      console.log(file, fileList);
    },
    getFile1(file, fileList) {
      this.uploadFile(file);
    },
    uploadFile(file) {
      var fd = new FormData();
      fd.append("crmId", this.drawerData.id);
      fd.append("crmType", this.drawerData.crmType);
      fd.append("file", file.raw);
      importEnclosureClue(fd).then((res) => {
        this.$message.success(res.msg);
        this.getEnclosure();
        this.$refs.uploadFile.clearFiles();
      });
    },
    // 操作记录
    getOperationList() {
      OperationRecord({
        columnId: this.drawerData.crmType,
        entryId: this.drawerData.id,
      }).then((res) => {
        this.Operation = res.data;
      });
    },
    // 更换负责人
    changeCharge() {
      this.$parent.drawerData = this.drawerData;
      this.$parent.changeCharge();
    },
    preview(row) {
      preview({
        url: row.fileUrl,
      }).then((res) => {
        this.fileHtml = res.msg;
        this.show = true;
      });
    },
    logShow(row) {
      this.renameShow = true;
      this.id = row.id;
    },
    rename() {
      rename({
        id: this.id,
        name: this.input,
      }).then((res) => {
        this.$message.success("修改成功");
        this.renameShow = false;
        this.getEnclosure();
        this.input = "";
      });
    },
    del(row) {
      delEnclosure({
        id: row.id,
      }).then((res) => {
        this.$message.success("删除成功");
        this.getEnclosure();
      });
    },
    getEnclosure() {
      getEnclosure({
        crmId: this.drawerData.id,
        crmType: this.drawerData.crmType,
      }).then((res) => {
        this.enclosureData = res.data;
      });
    },
    edit() {
      this.dialog.dialogClueShow = true;
      this.dialog.ejectClue = true;
      this.$emit("putUpdata", this.drawerData);
    },
    getfurther() {
      getActivity({
        crmId: this.drawerData.id,
        crmType: this.drawerData.crmType,
      }).then((res) => {
        this.furtherData = res.rows;
      });
    },
    drawerClose() {
      this.dialog.eject = false;
      this.dialog.activity = true;
      this.activityForm = {
        type: "",
        contactTime: "",
        content: "",
        crmId: this.drawerData.id,
        crmType: this.drawerData.crmType,
      };
    },
  },
};
</script>

<style lang="less" scoped>
// /deep/.el-drawer.rtl {
//   height: 96%;
//   margin-top: 4%;
// }
.head {
  padding: 29px;
  width: 100%;
  height: 155px;
  border-bottom: 1px solid #ececec;
  span {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #999999;
  }
  .header {
    display: flex;
    justify-content: space-between;
    .img {
      // transform: translateY(-15px);
      display: flex;
      .information {
        span {
          font-size: 12px;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
        }
        h4 {
          font-size: 16px;
          font-family: PingFangSC-Medium, PingFang SC;
          font-weight: 500;
          color: #2b2b2b;
          line-height: 16px;
        }
      }
      img {
        width: 40px;
        height: 40px;
        vertical-align: middle;
        margin-right: 12px;
      }
      h3 {
        display: inline-block;
      }
    }
  }
  .footer {
    margin-top: 20px;
    display: flex;
    .information {
      display: flex;
      flex-direction: column;
      margin-right: 80px;
      h4 {
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #2b2b2b;
        margin-top: 12px;
      }
    }
  }
}
.strip {
  width: 100%;
  height: 16px;
  background: #fafafa;
  border: 1px solid #ececec;
  /deep/.el-tabs__nav-scroll {
    background-color: #f5f8fa;
  }
}
/deep/ .el-tab-pane {
  padding-left: 24px;
}
.basics {
  position: relative;
  font-size: 14px;
}
.basics::before {
  content: "";
  position: absolute;
  left: -6px;
  top: 1px;
  width: 3px;
  height: 14px;
  background: #1267fa;
}

.system {
  margin-top: 70px;
  position: relative;
  font-size: 14px;
}
.system::before {
  content: "";
  position: absolute;
  left: -6px;
  top: 1px;
  width: 3px;
  height: 14px;
  background: #1267fa;
}
.basics-information {
  display: flex;
  flex-wrap: wrap;
  .subclass {
    width: 249px;
    margin-top: 30px;
    margin-right: 150px;
    span {
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #999999;
      line-height: 14px;
    }
  }
}
.operation {
  display: flex;
  .user {
    display: flex;
    .portrait {
      width: 30px;
      height: 30px;
      border-radius: 30px;
      img {
        width: 100%;
        height: 100%;
        border-radius: 30px;
      }
    }
    .name {
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      width: 100px;
      height: 30px;
      line-height: 30px;
      margin-left: 14px;
    }
  }
  .record {
    padding-left: 21px;
    margin-left: 44px;
    border-left: 1px dotted #dbdcdb;
    .time {
      font-size: 14px;
      position: relative;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #999999;
      line-height: 14px;
    }
    .time::before {
      content: "";
      position: absolute;
      left: -27px;
      width: 11px;
      height: 11px;
      background-color: #000;
      transform: rotate(45deg);
    }
    .content {
      padding-top: 10px;
      padding-bottom: 10px;
    }
  }
}
.activ {
  width: 100%;
  height: 100%;
}
.cz {
  color: #1267fa;
  margin-left: 5px;
}
.del {
  color: #fa1212;
  margin-left: 5px;
}
/deep/ .el-tabs__content {
  height: 78vh !important;
  overflow: auto;
}
/deep/.el-table {
  height: 520px;
}
.drawerClose {
  position: absolute;
  width: 40px;
  height: 40px;
  top: 189px;
  right: 68%;
  z-index: 9999;
  img {
    width: 100%;
    height: 100%;
    z-index: 9999;
  }
}
.team {
  display: flex;
  justify-content: space-between;
}
</style>